<?php
/**
 * Table Widget
 * User: ZFC
 * Date: 2015/7/30
 * Time: 15:44
 */

namespace common\widgets;


use yii\helpers\Html;

class Table extends Widget {

    /*
     * 表头数组
     * 当表格为垂直的时候会用到，但是当表格为水平的时候，次数据无效
     * [栏目1, 栏目2, 栏目3, ...]
     */
    public $columns;

    /*
     * 表格数据
     * 必须是二维数组
     * [
     *     ['a', 'b', 'c', ...]
     *     ['h', 'i', 'f', ...]
     *     ...
     * ]
     */
    public $data;


    protected $tdClass;
    protected $thClass;


    /*
     * 表格基本样式，垂直或者水平
     * horizontal, vertical
     */
    public $layout = 'vertical';


    public function init(){
        parent::init();

        if(isset($this->options['thClass']))
            $this->thClass = $this->options['thClass'];

        if(isset($this->options['tdClass']))
            $this->tdClass = $this->options['tdClass'];

    }


    public function run(){
        $content = '';
        if($this->layout == 'vertical'){
            $content = $this->buildTableHead() . $this->buildTableBody();
        }elseif($this->layout == 'vertical'){
            $content = $this->buildHorizontalTable();
        }

        return Html::tag('table', $content, $this->options);
    }


    /*
     * 构建表格Head
     */
    protected function buildTableHead(){
        $th = '';
        if(!empty($this->columns) && is_array($this->columns)){
            $th = $this->buildTh($this->columns);
        }

        return $th;
    }

    /*
     * 构建表格Body
     */
    protected function buildTableBody(){
        $tb = '';
        if(!empty($this->data) && is_array($this->data)){
            foreach($this->data as $k => $r){
                $tb .= '<tr>' . $this->buildTd($r) . '</tr>';
            }
        }

        return $tb;
    }

    /*
     * 构建TD
     */
    protected function buildTd($array){
        $td = '';
        foreach($array as $k => $v){
            $td .= Html::tag('td', $v, ['class'=>$this->tdClass]);
        }

        return $td;
    }

    /*
     * 构建Th
     */
    protected function buildTh($array){
        $td = '';
        foreach($array as $k => $v){

            $td .= Html::tag('th', $v, ['class'=>$this->thClass]);
        }

        return $td;
    }

    /*
     * 构建水平表格内容部分
     */
    protected function buildHorizontalTable(){
        $s = '';
        if(!empty($this->data) && is_array($this->data)){
            foreach($this->data as $r){
                if(!empty($r) && is_array($r)){
                    $tr = '<tr>';
                    foreach($r as $k => $v){
                        if($k == 0 || ($k % 2) == 0){
                            $tr .= Html::tag('th', $v, ['class'=>$this->thClass]);
                        }else{
                            $tr .= Html::tag('td', $v, ['class'=>$this->tdClass]);
                        }
                    }
                    $s .= $tr.'</tr>';
                }
            }
        }
        return $s;
    }

}